package edu.whu.rose.clock.kps.algorithm.blinks.search;

import java.util.ArrayList;

import com.sleepycat.je.DatabaseException;

import edu.whu.rose.clock.kps.algorithm.blinks.index.BilevelIndexDB;
import edu.whu.rose.clock.kps.algorithm.blinks.index.PNEntry;
import edu.whu.rose.clock.kps.datastruct.SimpleSubgraphVertex;

public class PNCursor extends Cursor {

	private final int portal;
	private final double base;
	private final int knode;
	private Cursor preCursors;
	
//	public PNCursor(int block, int portal, double base, int knode, BilevelIndexDB bidb) {
//		super(false, block, bidb);
//		this.portal = portal;
//		this.base = base;
//		this.knode = knode;
//		SimpleSubgraphVertex bk = new SimpleSubgraphVertex(block, portal);
//		try {
//			list = bidb.getFromPortalVertex(bk);
//			if (list == null) {
//				System.err.println("Error: cannot find PN list");
//				System.exit(1);
//			}
//		}
//		catch (DatabaseException ex) {
//			ex.printStackTrace();
//		}
//	}
	
	public PNCursor(int block, Cursor cursor, BilevelIndexDB bidb) {
		super(false, block, bidb);
		this.portal = cursor.getNode();
		this.base = cursor.getDistance();
		this.knode = cursor.getKnode();
		this.preCursors = cursor;
		SimpleSubgraphVertex bk = new SimpleSubgraphVertex(block, portal);
		try {
			list = bidb.getFromPortalVertex(bk);
			if (list == null) {
				System.err.println("Error: cannot find PN list");
				System.exit(1);
			}
		}
		catch (DatabaseException ex) {
			ex.printStackTrace();
		}
	}

	public int getFirst() {
		return ((PNEntry)list[pos]).getFirst();
	}

	public int getKnode() {
		return knode;
	}

	public int getNode() {
		return ((PNEntry)list[pos]).getNode();
	}

	public double getDistance() {
		return base + ((PNEntry)list[pos]).getDistance();
	}

	public int getPortal() {
		return portal;
	}

	public Cursor getPreCursors() {
		return preCursors;
	}

	public double peek() {
		return base + ((PNEntry)list[pos + 1]).getDistance();
	}

}
